#include "PrecompiledHeader_Engineer.h"
#include "CWorld.h"

/*--------------------------------------------------------------------------*/
float CWorld::m_quadSize = 50.0f;
/*--------------------------------------------------------------------------*/

CWorld::CWorld() :
	EngMod("CWorld", 0),
	IGameMode("world")
{
	this->AddRenderTask(CContext::getInstance()->GetContext("d2d"),
	                    CMaterial::getInstance()->GetNullMaterial(),
	                    CCamera::getInstance()->GetCamera("cam2d"),
	                    0,
	                    vl::Render::UserInterface,
	                    1000);

	this->AddRenderTask(CContext::getInstance()->GetContext("d2d"),
	                    CMaterial::getInstance()->GetMaterial("minimap"),
	                    CCamera::getInstance()->GetCamera("cam2d"),
	                    1,
	                    vl::Render::UserInterface,
	                    1001);

	this->AddRenderTask(CContext::getInstance()->GetContext("d2d"),
	                    CMaterial::getInstance()->GetMaterial("wholeWhite"),
	                    CCamera::getInstance()->GetCamera("cam2d"),
	                    2,
	                    vl::Render::UserInterface,
	                    1002);
}

/*--------------------------------------------------------------------------*/

CWorld::~CWorld()
{

}

/*--------------------------------------------------------------------------*/

void CWorld::OnCalc(unsigned int threadId)
{
	for (std::vector<IWorldMod*>::iterator iter = this->m_worldMods.begin(); iter != this->m_worldMods.end(); ++iter)
	{
		(*iter)->OnLevelCalc();
	}

	this->BindRenderTasks();
}

/*--------------------------------------------------------------------------*/

void CWorld::OnStart()
{
	for (std::vector<IWorldMod*>::iterator iter = this->m_worldMods.begin(); iter != this->m_worldMods.end(); ++iter)
	{
		(*iter)->OnLevelLoad("../Data/maps/1");
	}

	WPlayer::getInstance()->NextTarget();
}

/*--------------------------------------------------------------------------*/

void CWorld::OnStop()
{
	for (std::vector<IWorldMod*>::iterator iter = this->m_worldMods.begin(); iter != this->m_worldMods.end(); ++iter)
	{
		//(*iter)->OnLevelUnload();
	}

	//CTree::getInstance()->EraseTree();

	if (CTree::getInstance()->IsTreeEmpty() == false)
	{
		LogC(1) << "Tree is not empty after CWorld::OnStop";
		CShell::getInstance()->Execute("treelog()");
	}
	else
	{
		LogI(1) << "CWorld stopped correctly";
	}
}

/*--------------------------------------------------------------------------*/

void CWorld::OnInit()
{
	this->m_worldMods.push_back(WTerrain::getInstance());
	this->m_worldMods.push_back(WBullets::getInstance());
	this->m_worldMods.push_back(WMobs::getInstance());
	this->m_worldMods.push_back(WPlayer::getInstance());
	this->m_worldMods.push_back(WWater::getInstance());
	this->m_worldMods.push_back(WWeather::getInstance());
}

/*--------------------------------------------------------------------------*/

void CWorld::OnRender(const unsigned int i, vl::Render::LevelOfDetail quality)
{
	float map_size = CSettings::getInstance()->m_resolutionf.y * 0.25f;

	glm::vec2 a = glm::vec2(CSettings::getInstance()->m_resolutionf.x - map_size, CSettings::getInstance()->m_resolutionf.y - map_size);
	glm::vec2 b = CSettings::getInstance()->m_resolutionf;

	switch (i)
	{
	case 0:
	{
		OpenGL::PutText("Radiation", glm::vec2(10.0f, 10.0f), vl::White);
		OpenGL::DrawRectangle(glm::vec2(100.0f, 10.0f), glm::vec2(100.0f + 100.0f * WPlayer::getInstance()->GetPlayerRadiation(), 20.0f), vl::Green);

		OpenGL::PutText("Stamina", glm::vec2(10.0f, 25.0f), vl::White);
		OpenGL::DrawRectangle(glm::vec2(100.0f, 25.0f), glm::vec2(100.0f + 100.0f * WPlayer::getInstance()->GetPlayerStamina(), 35.0f), vl::Blue);

		OpenGL::PutText("Health", glm::vec2(10.0f, 40.0f), vl::White);
		OpenGL::DrawRectangle(glm::vec2(100.0f, 40.0f), glm::vec2(100.0f + 100.0f * WPlayer::getInstance()->GetPlayerHealth(), 50.0f), vl::Red);

		OpenGL::PutText("Ammo", glm::vec2(10.0f, 55.0f), vl::White);
		OpenGL::DrawRectangle(glm::vec2(100.0f, 55.0f), glm::vec2(100.0f + 100.0f * WPlayer::getInstance()->GetPlayerAmmo(), 65.0f), vl::Yellow);

		break;
	}

	case 1:
	{
		CShader::getInstance()->SetSampler("heightmap", WTerrain::getInstance()->GetHeightMap(), 1);
		OpenGL::DrawRectangle(a, b, vl::White);

		break;
	}

	case 2:
	{
		glm::vec2 v = glm::vec2(WPlayer::getInstance()->GetPlayerPosition().x / 1000.0f, WPlayer::getInstance()->GetPlayerPosition().y / 1000.0f) * map_size;
		glm::vec2 t = glm::vec2(WPlayer::getInstance()->GetTarget().x / 1000.0f, WPlayer::getInstance()->GetTarget().y / 1000.0f) * map_size;

		glm::vec2 playerCenter = a + v;
		glm::vec2 targetCenter = a + t;

		OpenGL::DrawLine(glm::vec2(playerCenter.x, a.y), glm::vec2(playerCenter.x, b.y), vl::Black);
		OpenGL::DrawLine(glm::vec2(a.x, playerCenter.y), glm::vec2(b.x, playerCenter.y), vl::Black);

		OpenGL::DrawRectangle(playerCenter - glm::vec2(3.0f, 3.0f), playerCenter + glm::vec2(3.0f, 3.0f), vl::White);

		OpenGL::DrawRectangle(targetCenter - glm::vec2(3.0f, 3.0f), targetCenter + glm::vec2(3.0f, 3.0f), vl::Blue);

		OpenGL::DrawLine(glm::vec2(a.x, a.y), glm::vec2(b.x, a.y), vl::Black);
		OpenGL::DrawLine(glm::vec2(a.x, a.y), glm::vec2(a.x, b.y), vl::Black);
		OpenGL::DrawLine(glm::vec2(b.x, b.y), glm::vec2(a.x, b.y), vl::Black);
		OpenGL::DrawLine(glm::vec2(b.x, b.y), glm::vec2(b.x, a.y), vl::Black);

		break;
	}

	}
}

/*--------------------------------------------------------------------------*/
